Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for Regexp validator set html input tag 'pattern' attribute #759

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lefty01
Copy link

@lefty01 lefty01 commented Nov 13, 2022

for Regexp validator set html input tag 'pattern' attribute (used if novalidate not set on a form)

Similar to the Length validator that sets the html input attributes minlength and maxlength I found it useful to have this for regex patterns as well.
One advantage of using the browser is to get the message translated already (even tough here it is a bit generic, since it only says input needs to match the requested format and does not tell about the format itself)

added some basic test ... not sure if that is accepted this way?

…novalidate not set on a form)

test that pattern shows up in the input field
@lefty01 lefty01 force-pushed the wtforms_validator_regex_set_html_attrib branch from 4b91b87 to b86c625 Compare November 13, 2022 19:09
@azmeuk
Copy link
Member

azmeuk commented Dec 24, 2022

Thank you for you contribution.
I think an additional test that actually generate some HTML would be good to have, maybe in the fashion of https://github.com/wtforms/wtforms/blob/7418bac10d689c8ca428e66d465317d9fcd7b215/tests/fields/test_html5.py#L148

Also, can you add a CHANGELOG entry?

For the record, the related doc : https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/pattern

def test_regexp_message(dummy_form, dummy_field, grab_error_message):
"""
Regexp validator should return given message
"""
validator = regexp("^a", message="foo")
dummy_field.data = "f"
assert grab_error_message(validator, dummy_form, dummy_field) == "foo"


def test_regexp_pattern_html(dummy_form, dummy_field):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure of the point of this test. I suppose the assertion could go on test_regex_passes.

@lefty01
Copy link
Author

lefty01 commented Jan 2, 2023

... need to check again .. I was not that sure about proper testing here. will take a closer look Thx!

@azmeuk azmeuk added the enhancement New feature, or existing feature improvement label Jul 27, 2023
@azmeuk
Copy link
Member

azmeuk commented Jul 27, 2023

From the MDN documentation.

The pattern attribute is an attribute of the text, tel, email, url, password, and search input types.

You would probably want to add the pattern flag to the concerned widgets too.

@azmeuk
Copy link
Member

azmeuk commented Oct 10, 2023

I was thinking that python and JS regex might have different features: see here or here.

In that case, a valid python regex might not be a valid JS regex. There are compatibility layers like https://pypi.org/project/js-regex/ (but this one is unmaintained).

As differences mainly concerns advanced regex, i am not sure this is a problem.

@azmeuk
Copy link
Member

azmeuk commented Oct 10, 2023

@lefty01 are you interested in finishing the PR or would you like to let someone else take it over?

@ThiefMaster
Copy link

IMHO this should be configurable. You might not always want to send the pattern to the client, or you may want to use regex features not supported in the browser.

@lefty01
Copy link
Author

lefty01 commented Oct 11, 2023

@lefty01 are you interested in finishing the PR or would you like to let someone else take it over?

well, interested yes of course but right now it's not on top of my list ;)
so I'm quite happy there's now further discussion ongoing and of course do not mind at all if you/someone takes over the item so we can close it at some point.
... been a while and its bit hard to follow up anyways ;)

@dougthor42
Copy link

FYI: In my experience with HTML pattern, there are two caveats:

  1. I've found that the HTML pattern string needs to be slightly different than a standard python re.pattern string. IME the difference is typcially related to escaping.
    • Example: Python accepts r"[-\w]*", but HTML pattern must be r"[\-\w]*"
  2. It doesn't seem possible to "clean" a verbose regex back to a bare pattern string, so any patterns that are passed to HTML pattern can't be verbose. See https://stackoverflow.com/q/35640871/1354930

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature, or existing feature improvement
Development

Successfully merging this pull request may close these issues.

4 participants